package cn.itsource.product.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.mapper.ProductDetailMapper;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //先删除product
        productMapper.delete(id);
        productDetailMapper.deleteByProductid(id);
    }

    /**
     * 根据id获取productDetail
     * @param id
     * @return
     */
    @Override
    public List<ProductDetail> selectProductDetail(Long id) {

        return productMapper.getProductDetail(id);
    }

    /**
     * 商品上架
     * @param products
     */
    @Override
    @Transactional
    public void onsale(List<Product> products) {
        productMapper.onsale(products);
    }

    @Override
    @Transactional
    public void offsale(List<Product> products) {
        productMapper.offsale(products);
    }

    /**
     * 根据id获取product和详情数据
     * @param id
     * @return
     */
    @Override
    public Product getProductById(Long id) {

        return productMapper.getProductById(id);
    }

    /**
     * 批量删除
     * @param products
     */
    @Override
    @Transactional
    public void batchRemove(List<Product> products) {
        //先删除product表
         productMapper.batchRemove(products);
         productDetailMapper.batchRemove(products);
    }

    /**
     * 添加
     * @param product
     */
    @Override
    @Transactional
    public void save(Product product) {
        //先添加Product
        productMapper.save(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct(product);
        //再添加详细预约表
        productDetailMapper.save(productDetail);
    }

    /**
     * 修改
     * @param product
     */
    @Override
    @Transactional
    public void update(Product product) {
        //先修改product表
        productMapper.update(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct(product);
        //再修改详细表
        productDetailMapper.update(productDetail);
    }

}
